﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using HtmlAgilityPack;
using System.Windows.Media.Imaging;

namespace MolfMolfMolf
{
    public partial class NewsDetailPage : PhoneApplicationPage
    {
        string uri;
        string imgUri;
        string title;
        string date;
        string text;

        public NewsDetailPage()
        {
            InitializeComponent();
            if (PhoneApplicationService.Current.State.ContainsKey("uri") && (PhoneApplicationService.Current.State.ContainsKey("imgUri")) 
                && (PhoneApplicationService.Current.State.ContainsKey("title"))&&(PhoneApplicationService.Current.State.ContainsKey("date")))
            {
                uri = (string)PhoneApplicationService.Current.State["uri"];
                imgUri = (string)PhoneApplicationService.Current.State["imgUri"];
                title = (string)PhoneApplicationService.Current.State["title"];
                date = (string)PhoneApplicationService.Current.State["date"];
                PageTitle.Text = title;
                
            }

            HtmlWeb web = new HtmlWeb();
            web.LoadCompleted += new EventHandler<HtmlDocumentLoadCompleted>(web_LoadCompleted);
            web.LoadAsync(uri);
            FacebookShare.Tap += new EventHandler<GestureEventArgs>(FacebookShare_Tap);

            

        }

        void FacebookShare_Tap(object sender, GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(String.Format("/FacebookAuth.xaml?link={0}", uri), UriKind.Relative));
        }

        void web_LoadCompleted(object sender, HtmlDocumentLoadCompleted e)
        {
            try
            {
                HtmlDocument document = e.Document;
                HtmlNodeCollection texts = document.DocumentNode.SelectNodes("//div[@class='news-text']");
                HtmlNodeCollection links = document.DocumentNode.SelectNodes("//div[@class='news-text']//a");
                HtmlNodeCollection dates = document.DocumentNode.SelectNodes("//div[@class='news-text']//strong[@class='publish-date']");
                HtmlNodeCollection images = document.DocumentNode.SelectNodes("//div[@class='news-image']/img");
                if (images != null)
                {
                    imgUri = "http://www.golf.fi" + images[0].GetAttributeValue("src", "");
                }
                text = texts[0].InnerText;
                text = text.Replace("&nbsp;", String.Empty);
                if (links != null)
                {
                    foreach (HtmlNode n in links)
                    {
                        text = text.Replace(n.InnerText, String.Empty);
                    }
                }
                if (dates != null)
                {
                    foreach (HtmlNode n in dates)
                    {
                        text = text.Replace(n.InnerText, String.Empty);
                    }
                }
                while (text.IndexOf("   ") != -1)
                {
                    text = text.Replace("   ", "  ");
                }
                text = text.Replace("  ", "\n\n");
                text = text.Remove(text.IndexOf("\n"), 1);

                NewsImage.Source = new BitmapImage(new Uri(imgUri, UriKind.Absolute));
            }
            catch
            {
                NewsImage.Source = null;
                //NewsImage.Source = new BitmapImage(new Uri("Images/default.jpg", UriKind.Relative));
            }
            finally
            {
                NewsDetails.Text = text;
                Date.Text = date;
            }
        }
    }
}
